clear all
set more off
set matsize 10000
tempfile ubigeo temp
set scheme s1color

local controls c.schooling male rural native married head 

use mergedp, clear

egen voted_bn = rowmax(voted_blank voted_null)
recode voted_blank voted_null voted_bn (. = 0) if voted == 0

** LABELS
label var todoAT "Violence Exposure"

******* Results
drop if vote_matter == .
local lista vote_matter voted voted_blank voted_null voted_bn partN dem_works dem_matters1 gov_demo1 gov_auto1

gen depnac1993 = int(provnac1993/100)
gen dep1993 = int(prov1993/100)

gen migration_dist = ubigeonac1993 != ubigeo1993 
gen migration_prov = provnac1993 != prov1993
gen migration_dep = depnac1993 != dep1993

gen dist_prov = migration_dist == 1 & migration_prov == 0
gen dist_dep = migration_dist == 1 & migration_prov == 1 & migration_dep == 0 

egen grupo = group(ubigeo1993 ubigeonac1993)

merge m:1 ubigeonac1993 using violenceAT_district
gen T = _merge == 3
drop _merge

gen year_b = year - age
keep if year_b <= 1980


recode year_b (-9/1959 = 0), gen(X)
replace X = X - 1959 if X > 0
sum X
local max = r(max)

save xx, replace

foreach var in `lista' {
	use xx, clear

	mat define X = J(`max',3,.)
	
	replace `var' = `var' * 100 if "`var'" != "partN"

	areg `var' `controls' i.age i.provid#c.trend i.year i.T##i.X, abs(grupo) vce(cluster ubigeonac1993) 

	forvalues x = 1/21 {
		mat define X[`x',1] = `x' + 1959
		mat define X[`x',2] = _b[1.T#`x'.X]
		mat define X[`x',3] = _se[1.T#`x'.X]
	}

	clear 
	svmat X
	
	gen X4 = "`var'"

	save X_`var', replace
}

erase xx.dta



local r = 1
foreach var in `lista' {
	if "`var'" == "voted" {
		local title "Voted"
		local x = -1.06
	}
	else if "`var'" == "vote_matter" {
		local title = "Votting Matters"
		local x = -1.33
	}	
	else if "`var'" == "voted_blank" {
		local title = "Voted Blank"
		local x = 0.03
	}	
	else if "`var'" == "voted_null" {
		local title = "Voted Null"
		local x = 0.15
	}	
	else if "`var'" == "voted_bn" {
		local title = "Voted Blank/Null"
		local x = 0.19
	}	
	else if "`var'" == "partN" {
		local title = "Participation Organizations"
		local x = 0.01
	}	
	else if "`var'" == "dem_works" {
		local title = "Democracy Works"
		local x = 0.32
	}	
	else if "`var'" == "dem_matters1" {
		local title = "Democracy Matters"
		local x = 0.19
	}	
	else if "`var'" == "gov_demo1" {
		local title = "Prefers Democratic Gov."
		local x = -0.17
	}	
	else if "`var'" == "gov_auto1" {
		local title = "Prefers Authocratic Gov."
		local x = 0.16
	}	
	
	if inlist(`r',9,10) {
		local xtitle "xtitle(Year Born, size(small))"
	}
	else {
		local xtitle
	}	

	use X_`var', clear
	gen up = X2 + 1.96 * X3
	gen lo = X2 - 1.96 * X3

	twoway ///
		(rcap up lo X1 , xtitle(, size(small)) xlabel(, labsize(small)) ylabel(, labsize(small)) lwidth(thin) lcolor(blue)) ///
		(scatter X2 X1 , yline(`x', lcolor(green) lwidth(thick)) xtitle(, size(small)) xlabel(, labsize(small)) msize(small) mcolor(blue)) ///
		(function y = 0, range(X1) lcolor(red)) ///
		, xlabel(1960(5)1980, labsize(vsmall)) legend(off) `xtitle' title(`title', size(small))
	
	graph save g`r'.gph, replace
	
	local ++r
}
	
graph combine g1.gph g2.gph g3.gph g4.gph g5.gph g6.gph g7.gph g8.gph g9.gph g10.gph, cols(2)
graph export Figure_3.png, replace height(1600) width(1800)


local r = `r' - 1

forvalues x = 1/`r' {
	erase g`x'.gph
}
